// $codepro.audit.disable numericLiterals, emptyMethod
/**
 * search panel
 */
package edu.gatech.oad.doctorsorder;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JPanel;

import edu.gatech.oad.ui.UserPanel;
import edu.gatech.oad.user.User;
import edu.gatech.oad.user.UserProfilePanel;

import javax.swing.AbstractAction;
import javax.swing.JTextField;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import java.awt.BorderLayout;

/**
 * User interface to display search results
 * 
 * @author chen
 * @version 1.0
 * 
 */
public class SearchPanel extends JPanel {

	/**
	 * declare variables
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * field for search key
	 */
	private final JTextField searchKeyField;

	/**
	 * controll to perform search
	 */
	private final SearchController sc;

	/**
	 * search results
	 */
	private final List<User> resultList;

	/**
	 * component to display results
	 */
	private final JList list;

	/**
	 * Create the panel.
	 * 
	 * @param usrList
	 *            the user list to search in
	 * @param usrPanel
	 *            the user panel to display
	 * @param currentUser
	 *            User
	 */
	public SearchPanel(final List<User> usrList, final UserPanel usrPanel,
			final User currentUser) {
		sc = new SearchController(usrList);
		resultList = new Vector<User>();
		setLayout(new BorderLayout(0, 0));

		final JPanel inputPanel = new JPanel();
		add(inputPanel, "North");
		inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.X_AXIS));

		searchKeyField = new JTextField();
		searchKeyField.setText("");
		searchKeyField.getDocument().addDocumentListener(new SearchListener());
		inputPanel.add(searchKeyField);
		searchKeyField.setColumns(20);

		final JButton btnSearch = new JButton("search");

		final JScrollPane scrollPane = new JScrollPane();
		add(scrollPane);

		list = new JList();
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		// add actionLister for search button and enter key
		btnSearch.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(final ActionEvent e) {
				// search for users when button clicked
				resultList.clear();
				resultList.addAll(sc.search(searchKeyField.getText()));

				list.setListData((Vector<User>) resultList);

			}
		});

		searchKeyField.getKeymap().addActionForKeyStroke(
				KeyStroke.getKeyStroke('\n'), new AbstractAction() {

					@Override
					public void actionPerformed(final ActionEvent arg0) {
						// search for users when enter pressed
						resultList.clear();
						resultList.addAll(sc.search(searchKeyField.getText()));

						final List<String> displayList = new ArrayList<String>();
						for (final User usr : resultList)
							displayList.add(usr.displayString());
						list.setListData(displayList.toArray());

					}
				});

		inputPanel.add(btnSearch);

		// add mouseListener for result list
		final MouseListener mouseListener = new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				if (e.getClickCount() == 2) {
					// action performed when double clicked an item in list
					final User selectedUser = resultList.get(list
							.locationToIndex(e.getPoint()));// the selected user
					
					usrPanel.addNewTab(selectedUser.getName(),
							new UserProfilePanel(currentUser, usrPanel,
									selectedUser));
					searchKeyField.setText("");
					resultList.clear();
				}
			}
		};
		list.addMouseListener(mouseListener);
		scrollPane.setViewportView(list);

	}

	/**
	 * add results to panel
	 */
	public void addResultsToList() {
		resultList.clear();
		resultList.addAll(sc.search(searchKeyField.getText()));

		final List<String> displayList = new ArrayList<String>();
		for (User usr : resultList)
			displayList.add(usr.displayString());
		list.setListData(displayList.toArray());
	}

	/**
	 * Documents listener
	 * 
	 * @author chen
	 * 
	 */
	private class SearchListener implements DocumentListener {
		/**
		 * Method changedUpdate.
		 * 
		 * @param e
		 *            DocumentEvent
		 * @see javax.swing.event.DocumentListener#changedUpdate(DocumentEvent)
		 */
		@Override
		public void changedUpdate(DocumentEvent e) {

		}

		/**
		 * Method removeUpdate.
		 * 
		 * @param e
		 *            DocumentEvent
		 * @see javax.swing.event.DocumentListener#removeUpdate(DocumentEvent)
		 */
		@Override
		public void removeUpdate(DocumentEvent e) {
			addResultsToList();
		}

		/**
		 * Method insertUpdate.
		 * 
		 * @param e
		 *            DocumentEvent
		 * @see javax.swing.event.DocumentListener#insertUpdate(DocumentEvent)
		 */
		@Override
		public void insertUpdate(DocumentEvent e) {
			addResultsToList();
		}

		/**
		 * Method toString.
		 * 
		 * @return String
		 */
		@Override
		public String toString() {
			return super.toString();
		}
	}

	/**
	 * To string method
	 * 
	 * @return String chart panel
	 */
	@Override
	public String toString() {

		return super.toString() + "chart panel";
	}

}